/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  getFansData, // 获取粉丝数据
  wxGetDataCenterList, // 获取数据列表
} from "@/services/platform";
import { isLegalData } from "@/utils/utils";

export default {
  namespace: "dataCenter",

  state: {
    operatorList: {
      list: [],
      pagination: {},
    },
    siteList: {
      list: [],
      pagination: {},
    },
    fansData: {},
  },

  effects: {
    // 数据中心，获取粉丝数据
    *getFansData({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getFansData, payload);
      if (!response) return;
      const { code, data = [], msg } = response;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            fansData: {
              weixin: data.filter(val => val.platformType == "weixin")[0],
              weixin_mini: data.filter(val => val.platformType == "weixin_mini")[0],
            },
          },
        });

        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 数据中心，获取业务员列表
    *wxGetDataCenterList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const { type } = payload;
      const response = yield call(wxGetDataCenterList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { list = [], total, page } = data;
      let num = 20 * (page - 1) + 1;
      const sequenceList = list.forEach(val => {
        val.num = num++;
      });

      if (code == 0) {
        if (type == "courier") {
          yield put({
            type: "save",
            payload: {
              operatorList: {
                list: isLegalData(sequenceList, []),
                pagination: {
                  total,
                  pageSize: 20,
                  page,
                },
              },
            },
          });
        } else {
          yield put({
            type: "save",
            payload: {
              siteList: {
                list: isLegalData(sequenceList, []),
                pagination: {
                  total,
                  pageSize: 20,
                  page,
                },
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        __dva_reject(code);
        code != 9999 && message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
